package com.hrs.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrs.common.result.Result;
import com.hrs.core.mapper.AchieveMapper;
import com.hrs.core.mapper.UserMapper;
import com.hrs.enity.db.Department;
import com.hrs.core.service.DepartmentService;
import com.hrs.core.mapper.DepartmentMapper;
import com.hrs.enity.ro.DepartmentAddRo;
import com.hrs.enity.ro.DepartmentUpdateRo;
import com.hrs.enity.vo.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 纱雾休比酱
* @description 针对表【t_department(部门表)】的数据库操作Service实现
* @createDate 2023-01-12 22:14:50
*/
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
    implements DepartmentService{

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private AchieveMapper achieveMapper;

    @Autowired
    private DepartmentTreeService departmentTreeService;

    @Override
    public Result<List<DepartmentVo>> listDepartment() {
        List<DepartmentVo> departmentVos = departmentMapper.listAll();
        List<DepartmentVo> result = departmentTreeService.buildDeptTree(departmentVos);
        return Result.success(result);
    }

    @Override
    public Result<List<DepartmentVo>> listAll() {
        List<DepartmentVo> departmentVos = departmentMapper.listAll();
        return Result.success(departmentVos);
    }

    @Override
    public Result<DepartmentVo> getDepartmentById(Integer deptId) {
        DepartmentVo departmentVo = departmentMapper.selectByDeptId(deptId);
        return Result.success(departmentVo);
    }

    @Override
    public Result<String> addDepartment(DepartmentAddRo departmentAddRo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentAddRo,department);
        this.save(department);
        department.setSort(department.getId());
        this.updateById(department);
        return Result.success();
    }

    @Override
    public Result<String> updateDepartment(DepartmentUpdateRo departmentUpdateRo) {
        if(departmentUpdateRo.getParentId() == null) {
            return Result.error("上级部门id不能等于当前部门id");
        }
        Department department = new Department();
        BeanUtils.copyProperties(departmentUpdateRo,department);
        this.updateById(department);
        return Result.success();
    }

    @Override
    public Result<String> delDepartment(Integer deptId) {
        Department department = this.getById(deptId);
        if(Objects.isNull(department)) {
            return Result.error("目标不存在");
        }

        // 查询是否有子部门
        Integer countSubDepartment = departmentMapper.countSubDepartment(deptId);
        if(countSubDepartment > 0) {
            return Result.error("存在子部门，不可删除");
        }

        Integer countByDepartmentId = achieveMapper.countByDepartmentId(deptId);
        if(countByDepartmentId > 0) {
            return Result.error("与档案有关联，不可删除");
        }

        this.removeById(deptId);
        return Result.success();
    }
}




